package cn.edu.zjicm.wordsnet_d.download;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.util.Log;
import cn.edu.zjicm.wordsnet_d.datapersistent.FileManager;
import cn.edu.zjicm.wordsnet_d.net.Client;

class DownloadThread extends Thread {
	private static final int MIN_NOTIFY_TIME = 500;

	private static List<Integer> urlIndexList = null;
	Random r = new Random();

	private boolean mIsDownloading;
	private DownloadCallBack mCallBack;
	private Context context;

	DownloadThread(Context context, DownloadCallBack callBack) {
		this.context = context;
		mCallBack = callBack;
	}

	public void setDownloading(boolean isDownloading) {
		mIsDownloading = isDownloading;
	}

	public boolean isDownloading() {
		return mIsDownloading;
	}

	public void setDownloadCallBack(DownloadCallBack callBack) {
		mCallBack = callBack;
	}

	public void removeDownloadCallBack() {
		mCallBack = null;
	}

//	public static void setUrlIndex() {
//		if (urlIndexList != null)
//			urlIndexList.clear();
//		else
//			urlIndexList = new ArrayList<Integer>();
//
//		for (int x = 0; x < Client.BASE_URL_LIST.length; x++)
//			urlIndexList.add(x);
//	}

//	static int i = 0;
//
//	void getI(int n) {
//		int index = r.nextInt(n);
//		i = urlIndexList.get(index);
//		urlIndexList.remove(index);
//		Log.i("DownloadThread", i + "");
//	}

	public void run() {
//		mIsDownloading = true;
//		OutputStream FOS = null;
//		InputStream is = null;
//		int downloadSuccess = 0;
//		try {
////			int n = urlIndexList.size();
////			if (n > 0) {
////				getI(n);
//				URL url = new URL(Client.BASE_VOICE_URL	+ FileManager.getVoiceZipfile(context));
//				URLConnection conn = url.openConnection();
//				conn.connect();
//				is = conn.getInputStream();
//				int fileSize = conn.getContentLength();// 根据响应获取文件大小
//				if (fileSize <= 0) { // 获取内容长度为0
//					throw new NoContentException("无法获知文件大小 ");
//				}
//				if (is == null) { // 没有下载流
//					throw new FileNotFoundException("没有下载流");
//				}
//				String downloadingVoiceFilePath = FileManager.getDownloadingVoicePath(context);
//				int fileMB = FileManager.getDownloadedVoiceFileSize(context);
//				FOS = new BufferedOutputStream(new FileOutputStream(downloadingVoiceFilePath), 512000); // 创建写入文件内存流，通过此流向目标写文件
//				byte buf[] = new byte[4096];
//				int downLoadFilePosition = 0;
//				int numread;
//				long lastNotifyTime = 0;
//				while ((numread = is.read(buf)) != -1 && mIsDownloading) {
//					long now = System.currentTimeMillis();
//					FOS.write(buf, 0, numread);
//					downLoadFilePosition += numread;
//					if (mCallBack != null
//							&& now - lastNotifyTime > MIN_NOTIFY_TIME) {
//						mCallBack.onProgress(downLoadFilePosition, fileSize);
//					}
//					// Log.e("download", downLoadFilePosition+"");
//				}
//				FOS.flush();
//				FOS.close();
//				is.close();
//
//				// finish
//				if (mIsDownloading && (downLoadFilePosition / 1024 / 1024 >= fileMB)) {
//					downloadSuccess = 1;
//					String downloadedVoiceFilePath = FileManager.renameDownloadCompelateFile(new File(downloadingVoiceFilePath));
//					FileManager.unZipDownloadedVoiceFile(new File(downloadedVoiceFilePath));
//					// FileManager.unZipFile(FileManager.getDownloadedVoiceFile(),
//					// FileManager.getVoicePath());
//				} else
//					downloadSuccess = 0;
////			}
//		} catch (NoContentException e) {
//			downloadSuccess = 2;
//			e.printStackTrace();
//			Log.e("Download error", "NoContentException");
//		} catch (MalformedURLException e) {
//			downloadSuccess = 2;
//			e.printStackTrace();
//			Log.e("Download error", "MalformedURLException");
//		} catch (FileNotFoundException e) {
//			downloadSuccess = 2;
//			e.printStackTrace();
//			Log.e("Download error", "FileNotFoundException");
//		} catch (IOException e) {
//			downloadSuccess = 2;
//			e.printStackTrace();
//			Log.e("Download error", "IOException");
//		} catch (Exception e) {
//			downloadSuccess = 2;
//			e.printStackTrace();
//			Log.e("Download error", "Exception");
//		} finally {
//			// if(downloadSuccess == 2 && urlIndexList.isEmpty()){
//			// downloadSuccess = 0;
//			// }
//
//			if (mCallBack != null) {
//				mCallBack.onFinish(downloadSuccess);
//				mCallBack = null;
//			}
//			mIsDownloading = false;
//			try {
//				FOS.close();
//				is.close();
//			} catch (Exception ex) {
//			}
//		}
	}
}
